<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AccessRuleManager
 *
 * @author Ganda
 * Date : 9 - 11 - 2013
 * 
 * Ganda (16 - 2 - 2014)
 * tambah default MenuGroupID Untuk User yang tidak LOGIN. [$menuGroupID = 0]
 */

class AccessRuleManager extends Controller{
    private $className;
    
    private function getDefaultAccessRules(){
        return array(
//                    array('allow',  // allow all users to perform 'index' and 'view' actions
//                            'actions'=>array('index','view'),
//                            'users'=>array('*'),
//                    ),
//                    array('allow', // allow authenticated user to perform 'create' and 'update' actions
//                            'actions'=>array('create','update'),
//                            'users'=>array('@'),
//                    ),
//                    array('allow', // allow admin user to perform 'admin' and 'delete' actions
//                            'actions'=>array('admin','delete'),
//                            'users'=>array('admin'),
//                    ),
                    array('deny',  // deny all users
                            'users'=>array('*'),
                            //'message'=>array('Anda tidak diijinkan untuk mengakses halaman ini!!'),
                    ),
            );
    }
    
    public function accessRules(){
        $accRule = array();
        $this->className = $this->id;
        
        if(!Yii::app()->user->isGuest){
            $menuGroupID = Sysusrsecurity::model()->find('credentialName = "'.Yii::app()->user->name.'" and isLocked = 0 and ExpiredDate > '.Date('Y-m-d'), 'applicationID, ModuleID, InterfaceID ASC')->MenuGroupID;            
            if(isset($menuGroupID)){
                $accRuleDB = Sysappaccessrule::model()->findAll('MenuGroupID = '.$menuGroupID.' and ControllerName = "'.$this->className.'" and isActive = 1');
                
                foreach ($accRuleDB as $ar){
                    $accRule[$ar->RuleGroupArray][0] = $ar->permissionAccess->PermissionAccessName;
                    if($ar->isArrayRuleValues){
                        $rvalue=array();
                        $arrRV = explode(',', $ar->RuleValues);
                        for($c=0;$c<count($arrRV);$c++){
                            array_push($rvalue, str_replace("'", '', $arrRV[$c]));
                        }
                        $accRule[$ar->RuleGroupArray][$ar->ruleType->RuleTypeName]=$rvalue;
                    }else{
                        $values = str_replace("'", '', $ar->RuleValues);
                        $values = str_replace('"', '', $ar->RuleValues);
                        $accRule[$ar->RuleGroupArray][$ar->ruleType->RuleTypeName]=$values;
                    }
                }
            }
        }else{
            $menuGroupID = 0;
            
            if(isset($menuGroupID)){
                $accRuleDB = Sysappaccessrule::model()->findAll('MenuGroupID = '.$menuGroupID.' and ControllerName = "'.$this->className.'" and isActive = 1');
                
                foreach ($accRuleDB as $ar){
                    $accRule[$ar->RuleGroupArray][0] = $ar->permissionAccess->PermissionAccessName;
                    if($ar->isArrayRuleValues){
                        $rvalue=array();
                        $arrRV = explode(',', $ar->RuleValues);
                        for($c=0;$c<count($arrRV);$c++){
                            array_push($rvalue, str_replace("'", '', $arrRV[$c]));
                        }
                        $accRule[$ar->RuleGroupArray][$ar->ruleType->RuleTypeName]=$rvalue;
                    }else{
                        $values = str_replace("'", '', $ar->RuleValues);
                        $values = str_replace('"', '', $ar->RuleValues);
                        $accRule[$ar->RuleGroupArray][$ar->ruleType->RuleTypeName]=$values;
                    }
                }
            }
        }
        
        if (empty($accRule)){
            $accRule = $this->getDefaultAccessRules();
        }
        
        return $accRule;
    }
    
}

?>
